#include "../DataStructures/list.h"

void ilist_copy(void *dest, const void *src) {
    *(int *) dest = *((int *) src);
}

void ilist_print(const void *n) {
    printf("%d ", *((int *) n));
    return;
}

struct add {
    int hno;
    char *name;
};

void clist_copy(void *dest, const void *src) {
    struct add *k = (struct add *) dest;
    k->hno = ((struct add *) src)->hno;
    strcpy(k->name, ((struct add*) src)->name);
}

void clist_print(const void *n) {
    printf("%d %s\n", ((struct add *) n)->hno, ((struct add*) n)->name);
    return;
}

void clist_delete(void *n) {
    free(((struct add*) n)->name);
    return;
}

void list_driver() {
    int k;
    int n;
    linklist ilist;
    struct add add;

#ifndef ONLINE_JUDGE
    freopen("list.in", "r", stdin);
#endif

    printf("Running Linked List simulation\n");

    printf("Reading a simple list of integers\n");
    list_init(&ilist, sizeof (int), NULL, ilist_copy, NULL, ilist_print);
    scanf("%d", &k);
    while (k--) {
        scanf("%d", &n);
        list_add(&ilist, &n);
    }

    list_traverse(&ilist);
    list_clean(&ilist);

    printf("\n\n");
    printf("Reading a complex list of addresses\n");
    list_init(&ilist, sizeof (struct add), NULL, clist_copy, clist_delete, clist_print);
    scanf("%d", &k);
    while (k--) {
        add.name = (char *) malloc(20);
        scanf("%d%s", &add.hno, add.name);
        list_add(&ilist, &add);
    }

    list_traverse(&ilist);

    list_clean(&ilist);
    printf("\n\n");
    printf("Reading another simple list of integers\n");
    list_init(&ilist, sizeof (int), NULL, ilist_copy, NULL, ilist_print);
    scanf("%d", &k);
    while (k--) {
        scanf("%d", &n);
        list_add(&ilist, &n);
    }

    list_traverse(&ilist);
    list_clean(&ilist);

}



